/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ResultEditor.ComplexRelationship;

import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import java.beans.PropertyChangeEvent;
import javax.swing.JComboBox;
import java.awt.Component;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.beans.PropertyChangeListener;
import java.util.Vector;
import javax.swing.AbstractCellEditor;
import javax.swing.JTable;
import javax.swing.JTextField;
import javax.swing.table.TableCellEditor;

/**
 * A custom cell editor for a JTable Cell
 * @author Chris
 */
public class PropertyCellEditor extends AbstractCellEditor implements TableCellEditor
{

    private String editvalue = null;
    private int row;
    private JTable table;
    private int column;
    private Editor father;

    /**
     * The Constructor for a PropertyCellEditor
     * @param father - the editor
     */
    public PropertyCellEditor(Editor father)
    {

        super();
        this.father = father;
    }

    /**
     * This will return the component for cell editing for each row and column
     * @param table - table for editing
     * @param value - value to edit
     * @param isSelected - true if the cell is selected
     * @param row - row of this cell
     * @param column - column of this cell
     * @return
     */
    public Component getTableCellEditorComponent(final JTable table, Object value,
            boolean isSelected, final int row, final int column)
    {
        //Set member variables
        this.row = row;
        this.table = table;
        this.column = column;

        // get data of this row
        final iListable thisrow = ((PropertyTableModel) table.getModel()).getRowData_inFormatOf_STableData(row);

        //If this column needs a combo box then build one.
        if (thisrow.needsCombo(column))
        {
            //Get values for this combo box and put them in an array
            String[] values;
            Vector<String> toUse = thisrow.getStrings(column);
            values = new String[toUse.size()];
            for (int i = 0; i < toUse.size(); i++)
            {
                values[i] = toUse.get(i);
            }
            //Build combo box
            final JComboBox box;
            //if values are null then bulid empty combo box
            if (values == null)
            {
                box = new JComboBox();
            }
            //if values are non null then build combo box with those values
            else
            {
                box = new JComboBox(values);
            }
            //If the cell is selected then set colors differently
            if (isSelected)
            {
                box.setForeground(table.getSelectionForeground());
                box.setBackground(table.getSelectionBackground());
            }
            //If not selected then set unselected colors
            else
            {
                box.setForeground(table.getForeground());
                box.setBackground(table.getBackground());
            }

            // Select the current value
            box.setSelectedItem(thisrow.getSelectedItem(column));

            //Allow for change of selection for Combo box items
            box.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent e)
                {

                    PropertyCellEditor.this.setCellEditorValue(box.getSelectedItem().toString());
                    thisrow.setString(column, box.getSelectedItem().toString());
                    table.update(table.getGraphics());

                }
            });
            /*
            box.addMouseListener(new java.awt.event.MouseAdapter()
            {

                public void mousePressed(java.awt.event.MouseEvent evt)
                {
                    pop();
                }
            });
             * 
             */
            return box;
        }
        //If this row/column does not need a combo box then just build a textfield
        //that can be edited
        else
        {
            //Create a textfield for the editor
            final JTextField jtextfield;
            jtextfield = new JTextField(thisrow.getSelectedItem(column));

            //If it can't be modified then set enabledToFalse
            if (!thisrow.isModifiable(column))
            {
                jtextfield.setEnabled(false);
            }
            jtextfield.addActionListener(new ActionListener()
            {

                public void actionPerformed(ActionEvent e)
                {
                    pop();
                    PropertyCellEditor.this.setCellEditorValue(jtextfield.getText());
                }
            });
            jtextfield.addPropertyChangeListener(new PropertyChangeListener()
            {

                public void propertyChange(PropertyChangeEvent evt)
                {
                    pop();
                    PropertyCellEditor.this.setCellEditorValue(jtextfield.getText());
                }
            });
            //Allow for editing by allowing keys to be typed.
            jtextfield.addKeyListener(new KeyListener()
            {

                public void keyTyped(KeyEvent e)
                {
                    pop();
                    PropertyCellEditor.this.setCellEditorValue((jtextfield.getText() + e.getKeyChar()).trim());
                }

                public void keyPressed(KeyEvent e)
                {
                    pop();
                }

                public void keyReleased(KeyEvent e)
                {
                }
            });

            return jtextfield;

        }

    }
    /**
     * Get the value in the cell editor
     * @return
     */
    public Object getCellEditorValue()
    {
        return this.editvalue;
    }
    /**
     * Set the value in the cell editor
     * @param value - the value to set the editor value to.
     */
    private void setCellEditorValue(String value)
    {
        this.editvalue = value;
        PropertyTableModel model = ((PropertyTableModel) table.getModel());
        model.setValueAt(value, row, column);

    }

    private void pop()
    {
        final iListable thisrow = ((PropertyTableModel) table.getModel()).getRowData_inFormatOf_STableData(row);
        if(thisrow == null)
            return;
        father.setDeleteEnabled(thisrow.couldDelete());
    }
}
